<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/17
 * Time: 上午10:50
 */

namespace app\common\model;

use app\common\model\Base;
use think\Validate;
use think\Db;

class Doctor extends Base
{

    protected $table = "doctor";
    public $base_arr = [
        'table' => 'doctor',
        'field' => 'name,password,phone,avatar,username,depart_id',
        'primary_key' => 'id'
    ];

    public $attach_arr = [
        [
            'table' => 'doctor_department',
            'field' => 'department',
            'primary_key' => 'id',
            'base_table_field' => 'depart_id'
        ],

    ];

    /**
     * 获取字段对应的数组
     * */
    public function getDataByMysql()
    {
        $data = [
            't_name' => $this->table,//必填
            'model' => '医生',//必填
            'name' => '姓名',
            'username' => '用户名',
            'password' => '密码',
            'phone' => '联系方式',
            'avatar' => '头像',
            'depart_id' => '医生部门id',
            'is_deleted' => '{"2":"功能禁用","1":"功能启用","3":"功能删除"}'
        ];
        return $data;
    }

    /**
     * 获取日志的访问方法
     **/
    public static function addDoctorAdminLog($list_action='',$form_data=[],$userId,$admin_type){
        $model = new Doctor();
        $actionName = strtolower(ACTION_NAME);
        $controller = strtolower(CONTROLLER_NAME);
        $old_data = [];
        $type = '';
        switch($actionName){
            //列表
            case "index":
                Base::save_admin_log($userId, $model, 'check','',[],[],$admin_type);
                break;

            case "add_doctor":
                $type = 'add';
                if ($model->request->isPost()) {
                    $form_data['password'] = md5($form_data['password']);
                }
                break;
            case "edit_doctor":

                $type = 'edit';
                if ($model->request->isPost()) {
                  /*  print_r($form_data['password']);
                    exit;*/
                    if($form_data['password']){
                        $form_data['password'] = md5($form_data['password']);
                    }else{
                        unset($form_data['password']);
                    }
                    $old_data = Base::getOneOldDataByTables($form_data['id'],$model->base_arr,$model->attach_arr);

                }
                break;
            case "del_doctor":
                $type = 'del';
                if ($model->request->isPost()) {
                    Db::execute("update doctor set is_deleted=3 where id=?", [$form_data['id']]);
                }
                break;
        }


        if($model->request->isPost() ){

            //$model 实例化之后的模型
            //$type 数据的操作类型
            //$form_data  form表单提交之后的表单
            //$url 跳转路径
            //$old_data 原有的数据
            //$attach_arr 附属表单数组
            //$primary_key 基本表单的主键值id
            $result =  Base::operaTable($model,$userId,$type,$form_data,$controller.'/'.$list_action,$old_data,$model->attach_arr,'id',$admin_type);
            return $result;
        }else if($type == 'detail'){
            $result =  Base::operaTable($model,$userId,$type,$form_data,$controller.'/'.$list_action,$old_data,$model->attach_arr,'id',$admin_type,false);
            return $result;
        }



        return true;


    }
}